package com.fpost.util;

import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionMapping;
import org.hibernate.HibernateException;

import com.fpost.constants.ActionConstants;
import com.fpost.constants.CommonConstants;
import com.fpost.domain.Ip;
import com.fpost.form.dto.UserDTO;
import com.fpost.manager.IpManager;
import com.fpost.security.ActionSecurityUtils;

public class CommonUtils {
	
	public static String privateIpAddress(String ipAddress) {
		final String DOT = ".";
		String[] array = ipAddress.split("\\.");
		return array[0] + DOT + array[1] + DOT + array[2] + DOT + "---";
	}
	
	public static void saveIpAddress(Long articleId, HttpServletRequest request) throws HibernateException {
		UserDTO userDTO = ActionSecurityUtils.getUserDTO_fromSession(request);
		Ip ip = new Ip(articleId, request.getRemoteAddr(), request.getRequestURI(),
				DateUtil.getGMTCalendar().getTime(), userDTO == null ? null : Long.parseLong(userDTO.getUserId()));
		new IpManager().save(ip);
	}
	
	public static void setCurrentPathInSession(HttpServletRequest request, ActionMapping mapping) {
//		System.out.println("URI = " + request.getRequestURI());
//		System.out.println("URL = " + request.getRequestURL());
//		System.out.println("PathInfo = " + request.getPathInfo());
//		System.out.println(request.getParameterMap());
//		System.out.println("ServletPath = " + request.getServletPath());
		
		String servletPath = request.getServletPath();
		Map<String, String[]> map = request.getParameterMap();
		String params = "";
		if (!map.isEmpty()) {
			params += "?"; 
			for (String key : map.keySet()) {
				params+= key + "=" + map.get(key)[0] + "&";
			}
			params = params.substring(0, params.length()-1);
		}
		//System.out.println(servletPath + params);
		request.getSession().setAttribute(ActionConstants.CURRENT_PATH, servletPath + params); 
	}
	
	public static ResourceBundle getResourceBundle(HttpServletRequest request) {
		//String lang = (String)request.getSession().getAttribute(ActionConstants.CURRENT_LANG);
		return ResourceBundle.getBundle("ApplicationResources",	
				(Locale)request.getSession().getAttribute(Globals.LOCALE_KEY));
	}
	
	public static String replaceLineSeparator(String str){
		return str.replaceAll(System.lineSeparator(), CommonConstants.NEW_LINE); 
	}
	
	public static String restoreLineSeparator(String str){
		return str.replaceAll(CommonConstants.NEW_LINE, System.lineSeparator()); 
	}
	
	public static String replaceNewline(String str){
		return str.replaceAll(CommonConstants.NEW_LINE, CommonConstants.LINE_BREAK); 
	}
	
	@Deprecated
	private static String escapeBr(String original) {
		if (original == null)
			return "";
		StringBuffer out = new StringBuffer("");
		char[] chars = original.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			switch (chars[i]) {
				case '\n': out.append("<br/>"); break; // newline
				case '\r': break;
				default  : out.append(chars[i]); break;
			}
		}
		return out.toString();
	}
}
